#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/wait.h>

int main()
{
  //环境变量的指针声明
  extern char**environ;

  printf("我是父进程，我的pid：%d\n",getpid());
  pid_t id=fork();  //创建子进程
  if(id==0)
  {
    //子进程
    //这里我们想让子进程执行全新的程序，以前是执行父进程代码片段
   
    printf("我是子进程，我的pid: %d,我的ppid: %d\n",getpid(),getppid());
    char *const env_[] = {
      (char*)"MYPATH=You Can See Me!",
      NULL 
    };
    execle("./mytest","mytest",NULL,environ);
    //execle("./mytest","mytest",NULL,env_);

    //execl("/home/study/lesson/lesson17/mytest","mytest",NULL); //使用绝对路径查找到目标程序所在位置
    //execl("/usr/bin/bash","bash","test.sh",NULL);
    //execl("/usr/bin/python3","python3","test.py",NULL);
    //execl("./mytest","mytest",NULL);   //使用相对路径查找到目标程序所在位置
    //execl("/home/study/lesson/lesson17/mytest","mytest",NULL); //使用绝对路径查找到目标程序所在位置

    //char *const argv_[] = {(char*)"ls",(char*)"-a",(char*)"-l",(char*)"-i",NULL};
    //execvp("ls",argv_);
    //execlp("ls","ls","-a","-l",NULL);  //这里的两个ls,含义不一样!
    //execv("/usr/bin/ls",argv_);
    //execl("/usr/bin/ls","ls","-l","-a",NULL);    //带选项
    exit(1);  //只要执行了exit，意味着excel系列的替换函数失败了！
  }
  //父进程
  int status = 0;
  int ret = waitpid(id,&status,0);
  if(ret==id)
  {
    sleep(2);
    printf("父进程等待成功！\n");
  }



  //printf("我是一个进程，我的pid：%d\n",getpid());
  //ls -a -l
  //int ret= execl("/usr/bin/lsdf","ls","-l","-a",NULL);    //带选项
  //execl("/usr/bin/top","top",NULL);            //不带选项
  //execl("/usr/bin/pwd","pwd",NULL);              //不带选项
  //printf("我执行完毕了，我的pid：%d,ret: %d\n",getpid(),ret);
  return 0;
}
